<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use think\Db;

/**
 * 用户认证
 *
 * @icon fa fa-users
 */
class Usersauth extends Backend {

    /**
     * Users模型对象
     * @var \app\admin\model\Users
     */
    protected $model = null;

    public function _initialize() {
        parent::_initialize();
        $this->model = new \app\admin\model\Users;
    }

    /**
     * 查看
     */
    public function index() {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
//            $otherwhere[] = ['referee_id', '=', $this->iAdminId];
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                    ->where($where)
                    ->order($sort, $order)
                    ->count();

            $list = $this->model
                    ->where($where)
                    ->order($sort, $order)
                    ->limit($offset, $limit)
                    ->select();
            $list = collection($list)->toArray();
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }

    public function updateWallet() {
        $wallet_id = input('wallet_id', 0);
        $type = input('type', 0, 'intval');
        $price = input('price', 0);
        $remark = input('remark');

        $arrLogData['wallet_id'] = $wallet_id;
        $arrLogData['change_type'] = $type;
        $arrLogData['change_matters'] = 3;
        $arrLogData['change_amount'] = $price;
        $arrLogData['remark'] = $remark;
        db('wallet_log')->insert($arrLogData);
        if ($type == 0) {
            db('wallet')->where(['wallet_id' => $wallet_id])->setInc('balance', $price);
        } else {
            db('wallet')->where(['wallet_id' => $wallet_id])->setDec('balance', $price);
        }
        $this->success('操作成功');
    }

    public function updateScore() {
        $user_id = input('user_id', 0);
        $type = input('type', 0, 'intval');
        $num = input('num', 0);
        $remark = input('remark');

        $arrLogData['user_id'] = $user_id;
        $arrLogData['change_type'] = $type;
        $arrLogData['change_matters'] = 3;
        $arrLogData['change_amount'] = $num;
        $arrLogData['remark'] = $remark;
        db('score_log')->insert($arrLogData);
        if ($type == 0) {
            db('users')->where(['user_id' => $user_id])->setInc('score', $num);
        } else {
            db('users')->where(['user_id' => $user_id])->setDec('score', $num);
        }
        $this->success('操作成功');
    }

    public function updatestatus() {
        $user_id = input('ids');
        $userInfo = db('users')->where('user_id', 'in', $user_id)->find();
        if (!$userInfo) {
            $this->error('无该用户');
        }
        $coupon_id = db('configure')->where('key', 'shimingrenzhenyouhuiquan')->value('desc');
        //绑定手机号优惠券
        if ($coupon_id) {
            $user_ids = explode(',', $user_id);
            foreach ($user_ids as $val) {
                receiveCoupon($val, $coupon_id);
            }
        }

        $data['user_status'] = 1;
        $data['updatetime'] = date('Y-m-d H:i:s');

        db('users')->where('user_id', 'in', $user_id)->update($data);



        $this->success();
    }

    public function refusal() {
        $user_id = input('ids');
        $userInfo = db('users')->where('user_id', 'in', $user_id)->find();

        if (!$userInfo) {
            $this->error('无该用户');
        }

        $refusal_reason = input('refusal_reason');
        if (empty($refusal_reason)) {
            $this->error('请填写拒绝原因');
        }
        $data['user_status'] = 10;
        $data['refusal_time'] = date('Y-m-d H:i:s');
        $data['refusal_reason'] = input('refusal_reason');

        db('users')->where('user_id', 'in', $user_id)->update($data);
        $this->success();
    }

}
